﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using SBACN.WebSite.Common;

namespace SBACN.WebSite.UC
{
	public partial class Menu : BaseControl
	{
		public int MenuIndex
		{
			get
			{
				if (ViewState["MenuIndex"] == null)
					return 0;
				return Convert.ToInt32(ViewState["MenuIndex"]);
			}
			set
			{
				ViewState["MenuIndex"] = value;
			}
		}

		private bool FindCurMenu = false;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				InitCtrl();
			}
		}

		private void InitCtrl()
		{
			var headerMenu = SystemMapCache.Instance[MenuCode];
			if (headerMenu != null)
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("<div id=\"leftnavheader\">");
				sb.Append(headerMenu.CText);
				sb.Append("</div>");
				sb.AppendLine("<div class='leftnavborder' >");

				var menuList = SystemMapCache.Instance.List.Where(p => p.Status && p.ShowInMap && p.Code.StartsWith(MenuCode) && p.Code.Length > MenuCode.Length).OrderBy(p => p.SortIndex).ThenBy(p => p.Code).ToList();
				MenuIndex = 0;
				foreach (var item in menuList.Where(p => p.Code.Length == MenuCode.Length + 4))
				{
					sb.AppendLine(CreateChildMenu(menuList, item));
				}
				sb.AppendLine("</div>");
				this.leftnav.InnerHtml += sb.ToString();
			}
		}

		private string CreateChildMenu(IList<TB_SystemMap> list, TB_SystemMap main)
		{
			string LockPath = "<img src=\"/images/lock_sm.png\" height=\"12px\" width=\"12px\" />";
			StringBuilder sb = new StringBuilder();
			var children = list.Where(p => p.Code.StartsWith(main.Code) && p.Code.Length > main.Code.Length);
			if (children.Count() > 0)
			{
				// 添加锁图标
				string LeftMenuMainText = "";
				if (main.NeedMemberLogin)// 需要会员登录
				{
					LeftMenuMainText = main.CText.CutTo(11, 10) + LockPath;
				}
				else// 不需要会员登录
				{
					LeftMenuMainText = main.CText.CutTo(12, 11);
				}

				sb.AppendLine("<div" + (main.Code.Length == 8 ? " class=\"mainLeft\" " : string.Empty) + "><a " + (main.Code.Length == 8 && SystemCode.StartsWith(main.Code) ? " style=\"color:#228BDF\" " : string.Empty) + "href='javascript:void(0);'>");
				sb.Append(LeftMenuMainText);
				sb.Append("</a></div>");
				sb.AppendLine("<div" + (main.Code.Length == 8 ? " class='list2' " : "") + ">");
				foreach (var child in children)
				{
					if (!FindCurMenu && child.Code == SystemCode)
					{
						FindCurMenu = true;
					}


					string LeftMenuChildText = "";
					// 添加锁图标
					if (child.NeedMemberLogin)// 需要权限
					{
						LeftMenuChildText = child.CText.CutTo(8, 7) + LockPath;
					}
					else// 不需要权限
					{
						LeftMenuChildText = child.CText.CutTo(9, 8);
					}
					sb.AppendLine("<div class='leftItem'> · <a title='" + child.CText + "' " + (child.Code == SystemCode ? " class=\"selected\" " : string.Empty) + " href=\"" + BasePage.UrlAppendQueryString(child.CURL, "st", child.Code, false) + "\" " + (child.CURL.ToLower().StartsWith("http://") ? " target='_blank' " : string.Empty) + ">" + LeftMenuChildText + "</a></div>");

				}
				sb.AppendLine("</div>");
				if (!FindCurMenu)
					MenuIndex++;
			}
			else
			{
				string LeftMenuText = "";
				// 添加锁图标
				if (main.NeedMemberLogin)// 需要权限
				{
					LeftMenuText = main.CText.CutTo(11, 10) + LockPath;
				}
				else// 不需要权限
				{
					LeftMenuText = main.CText.CutTo(12, 11);
				}

				sb.AppendLine("<div" + (main.Code.Length == 8 ? " class=\"mainLeftNoClild\" " : string.Empty) + "><a " + (main.Code.Length == 8 && SystemCode.StartsWith(main.Code) ? " style=\"color:#228BDF\" " : string.Empty) + "title='" + main.CText + "' href='" + BasePage.UrlAppendQueryString(main.CURL, "st", main.Code, false) + "' " + (main.CURL.ToLower().StartsWith("http://") ? " target='_blank' " : string.Empty) + ">" + LeftMenuText + "</a></div>");
			}
			return sb.ToString();
		}
	}
}